<template>
  <el-header class="me-area">
    <el-row class="me-header">
      <el-col :span="2" :offset="2" class="me-header-left">
        <router-link to="/" class="me-title">
          <img src="../assets/img/logo.png" />
        </router-link>
      </el-col>

      <el-col v-if="!simple" :span="12" :offset="0">
        <el-menu
          :router="true"
          menu-trigger="click"
          active-text-color="#5FB878"
          :default-active="activeIndex"
          mode="horizontal"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/tag/all">标签</el-menu-item>
          <!-- <el-menu-item index="/archives">文章归档</el-menu-item> -->
          <!-- <el-menu-item index="/log">日志</el-menu-item> -->
          <el-menu-item index="/1">我的博客</el-menu-item>
          <el-menu-item index="/2">站主博客</el-menu-item>
          <el-menu-item index="/3">我的收藏</el-menu-item>
          <!-- <el-menu-item index="/feedback">留言板</el-menu-item> -->

          <el-col :span="2" :offset="1">
            <el-menu-item index="/write"
              ><i class="el-icon-edit"></i>写文章</el-menu-item
            >
          </el-col>
        </el-menu>
      </el-col>

      <template v-else>
        <slot></slot>
      </template>

      <el-col :span="4" :offset="0">
        <el-menu
          :router="true"
          menu-trigger="click"
          mode="horizontal"
          active-text-color="#5FB878"
        >
          <template v-if="!user.login">
            <el-menu-item index="/login">
              <el-button type="text">登录</el-button>
            </el-menu-item>
            <el-menu-item index="/register">
              <el-button type="text">注册</el-button>
            </el-menu-item>
          </template>

          <template v-else>
            <el-submenu index>
              <template slot="title">
                <img class="me-header-picture" :src="user.avatar" />
              </template>
              <el-menu-item index @click="logout"
                ><i class="el-icon-back"></i>退出</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-col>
    </el-row>
  </el-header>
</template>

<script>
import {getToken, getUserInfo} from '@/request/token' 
export default {
  name: "BaseHeader",
  props: {
    activeIndex: String,
    simple: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  computed: {
    user() {
      let login = (getToken() != undefined && getToken() != '');
      let avatar = "";
      if (getUserInfo()) {
        avatar = JSON.parse(getUserInfo()).avatar;
      }
      console.log(getToken())
      console.log(login);
      return {
        login,
        avatar,
      };
    },
  },
  methods: {
    logout() {
      const _this = this;
      _this.$axios
        .post(
          "/logout",
          {},
          {
            headers: {
              Authorization: localStorage.getItem("token"),
            },
          }
        )
        .then((res) => {
          _this.$store.commit("REMOVE_INFO");
          _this.$router.push("/login");
        });
    },
  },
};
</script>

<style>
.el-header {
  position: fixed;
  z-index: 1024;
  min-width: 100%;
  box-shadow: 0 2px 3px hsla(0, 0%, 7%, 0.1), 0 0 0 1px hsla(0, 0%, 7%, 0.1);
}

.me-title {
  margin-top: 10px;
  font-size: 24px;
}

.me-header-left {
  margin-top: 10px;
}

.me-title img {
  max-height: 2.4rem;
  max-width: 100%;
}

.me-header-picture {
  width: 36px;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 50%;
  vertical-align: middle;
  background-color: #5fb878;
}
</style>
